import filters, { filterText, constFilter } from "@/filters";
import commonFn from "@/utils/common";
import Print from "vue-print-nb"; //打印
import { showFormErr, resetFields } from "@/utils";
import { checkAuth } from "@/cool/modules/base/directives";
import CONSTMAP from "@/constant";

const globalFun = {
	install(Vue) {
		// 批量通用方法
		Object.keys(commonFn).forEach(cKey => {
			Vue.prototype[cKey] = commonFn[cKey];
		});

		// 批量注入filter
		Object.keys(filters).forEach(fKey => {
			Vue.filter(fKey, filters[fKey]);
		});
		Vue.prototype.$filterText = filterText
		Vue.prototype.$constFilter = constFilter

		// 打印
		Vue.use(Print);

		Vue.prototype.$bus = new Vue();
		Vue.prototype.showFormErr = showFormErr;
		Vue.prototype.$auth = checkAuth;
		Vue.prototype.$resetFields = resetFields
		Vue.prototype.$const = CONSTMAP


		Vue.directive('enterNumber', {
			bind(el, { value = 2 }) {
				el = el.nodeName == "INPUT" ? el : el.children[0]
				let regStr = value == 0 ? `^[\\+\\-]?\\d+\\d{0,0}` : `^[\\+\\-]?\\d+\\.?\\d{0,${value}}`;
				el.addEventListener('keyup', () => {
					el.value = el.value.match(new RegExp(regStr, 'g'));
					el.dispatchEvent(new Event('input'))
				})
			}
		});

		Vue.directive('focus', {
			// 当被绑定的元素插入到 DOM 中时
			inserted(el) {
				el.focus()
			}
		});

	}
};
export default globalFun;
